﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace JKDD
{
    public class operateDB
    {
        private static SqlConnection con;
        private static SqlCommand com;

        public operateDB()
        {
            
        }

        public static DataSet GetDataSet(string sql)
        {
            con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
            com = new SqlCommand(sql, con);

            SqlDataAdapter adpt = new SqlDataAdapter();
            adpt.SelectCommand = com;
            DataSet ds = new DataSet();

            con.Open();
            try
            {
                adpt.Fill(ds);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
            }
            return ds;
        }

        public static DataTable GetDataTable(string sql)
        {
            con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
            com = new SqlCommand(sql, con);
            con.Open();
            DataTable dt = new DataTable();
            SqlDataReader dr;
            try
            {
                dr = com.ExecuteReader();
                dt.Load(dr);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
            }
            return dt;
        }

        public static int ExcuteSql(string sql)
        {
            con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
            com = new SqlCommand(sql, con);

            int result = 0;
            con.Open();
            SqlTransaction tran = con.BeginTransaction();
            com.Transaction = tran;
            try
            {
                result = com.ExecuteNonQuery();
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                throw ex;
            }
            finally
            {
                con.Close();
            }
            return result;
        }
    }
}
